package com.example.ttt.functions;

import com.example.ttt.jcomp.JFrameComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class DoubleClickPlay {

    @Autowired
    private Environment environment;
    @Autowired
    private SetPlayStatus setPlayStatus;
    @Autowired
    private JFrameComponent jFrameComponent;

    public void start(JTable table){
        /*
        * 1.获取播放index
        * 2.获取播放音乐的名称
        * 3.设置播放按钮的状态
        * */
        int selectedRow = table.getSelectedRow();
        String scannerFilePath = environment.getProperty("scannerFilePath");
        log.info("当前选中{}行",selectedRow);

        JukeCopy jukeCopy = jFrameComponent.getJukeCopy();
        jukeCopy = new JukeCopy(scannerFilePath);
        jukeCopy.startCopy(selectedRow);

        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Object currentSound = jukeCopy.getCurrentSound();
        if (Objects.nonNull(currentSound)){
            // 播放中
            jFrameComponent.setPlayStatus(2);
            jFrameComponent.setPlayActivity(true);
            setPlayStatus.start();
        }
    }

}
